package com.example.demo.leetcode.top100;

import java.util.Stack;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _69有效的括号
 * @description
 * @date 2023/06/24 9:57
 * <p>
 * ******************************************************
 */
public class _69有效的括号 {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();

        for (char c : s.toCharArray()) {
            if ('(' == c || '[' == c || '{' == c) {
                stack.push(c);
            } else {
                if (!stack.isEmpty() && stack.peek().equals(transfer(c))) {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }


    private char transfer(char c) {
        char res;
        switch (c){
            case ')':
                res = '(';
                break;
            case ']':
                res = '[';
                break;
            case '}':
                res = '{';
                break;
            default:
                res = c;
                break;
        }
        return res;
    }
}
